import random
import sys
import time


"""
该程序用于批量生成指定类型的随机数据库数据
"""

print("{:=^60}".format("生成数据库数据"))
print("注意:该程序较为简单只能用于最多三列不同类型的数据库表的数据生成!")
type_data = []
type_data_range = {}
for i in range(100):
    item = input(f"请输入第{i+1}列的数据类型\n如下选择:'int','char','float'\n")
    if item not in ["int", "char", "float"]:
        print("类型错误,程序结束!")
        sys.exit()
    type_data.append(item)

    if item == "int":
        print("请输入整数的范围:")
        range_int_front = int(input("起始数字:\n"))
        range_int_back = int(input("结束数字:\n"))
        type_data_range["range_int_front"] = range_int_front
        type_data_range["range_int_back"] = range_int_back
    elif item == "char":
        print("请输入字符串序列用于产生随机字符串数据:")
        char_list = []
        for i in range(100):
            item = input(f"请输入第{i+1}个字符串:\n例如:'张三','李四','王五'\n")
            char_list.append(item)
            choice = input("继续添加字符串序列请按'1'\n结束请按'2'\n")
            if choice == "2":
                break
        type_data_range["range_char"] = char_list
    elif item == "float":
        print("请输入浮点数的范围:")
        range_float_front = float(input("起始数字:\n"))
        range_float_back = float(input("结束数字:\n"))
        type_data_range["range_float_front"] = range_float_front
        type_data_range["range_float_back"] = range_float_back

    choice = input("继续添加下一列数据类型请按'1'\n结束请按'2'\n")
    if choice == "2":
        break

total = int(input("请输入要生成数据的总个数:\n"))

print("=======================开始生成数据=======================")

total_data = []

for i in range(total + 1):
    all_type = {}
    for one in type_data:
        if one == "int":
            random_int = random.randint(
                type_data_range["range_int_front"],
                type_data_range["range_int_back"],
            )
            all_type["int"] = random_int
        elif one == "char":
            random_fruit = random.choice(type_data_range["range_char"])
            all_type["char"] = random_fruit
        elif one == "float":
            random_float = random.uniform(
                type_data_range["range_float_front"],
                type_data_range["range_float_back"],
            )
            all_type["float"] = random_float
    total_data.append(all_type)

    # 加载进度条
    progress = "=" * i + "-" * (total - i)  # 已完成任务量和未完成任务量的图形显示
    percent = (i / total) * 100  # 完成度的百分比计算

    # 使用 \r 实现实时刷新
    print(f"[{progress}]-{percent:.2f}%", end="\r")

    time.sleep(0.1)  # 控制进度条下载的速度

print("\n =======================数据生成完毕=======================")

print("=======================开始输出数据=======================")

print("请选择输出方式:")
print("1.以表格的形式输出")
print("2.以代码的形式输出")
print("请选择'1'或'2'")

put = input()
print()

if put == "1":
    for line in total_data:
        for item, data in line.items():
            print(data, end="\t")
        print()
elif put == "2":
    table_name = input("请输入表名:\n")
    for line in total_data:
        table_data = []
        for item,data in line.items():
            table_data.append(data)
        table_data_tuple = tuple(table_data)
        print(f"INSERT INTO {table_name} VALUES {table_data_tuple};")
else:
    print("错误选项!")

print("=======================程序运行结束=======================")
